package com.example.performance.r2dbc.entity.performance;

import com.example.performance.r2dbc.entity.RecordDb;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.security.InvalidParameterException;
import java.sql.ResultSet;
import java.sql.SQLException;

import static com.example.performance.r2dbc.entity.performance.DDSFieldType.*;
import static com.example.performance.r2dbc.entity.performance.RecordSetUtil.*;

/**
 * DDSFieldId class
 *
 * @author ShiSheng Gao
 * @since 2023/04/11
 */
@Getter
@AllArgsConstructor
public enum DDSFieldId {

    /**
     * dds field id
     */
    ID_COMMANDE(0, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.commandeId(getString(rs, columnIndex));
        }
    },
    ID_CLIENT(1, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.clientId(getString(rs, columnIndex));
        }
    },
    DATE_EXPED_DEMAND(2, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.expedDemand(getLong(rs, columnIndex));
        }
    },
    DATE_DLUO(3, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.dluo(getLong(rs, columnIndex));
        }
    },
    DATE_PROD(4, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.prod(getLong(rs, columnIndex));
        }
    },
    DATE_LIVRAISON(5, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.livraison(getLong(rs, columnIndex));
        }
    },
    QTE_COMMANDE(6, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.commandQty(getDouble(rs, columnIndex));
        }
    },
    QTE_LIVRE(7, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.livre(getDouble(rs, columnIndex));
        }
    },
    QTE_RELIQUAT(8, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.reliquat(getDouble(rs, columnIndex));
        }
    },
    STATUS(9, null) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.statut(getStatus(rs, columnIndex));
        }
    },

    DATE_PROD_INIT(10, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.prodInit(getLong(rs, columnIndex));
        }
    },

    TEXT1(11, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text1(getString(rs, columnIndex));
        }
    },
    TEXT2(12, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text2(getString(rs, columnIndex));
        }
    },
    TEXT3(13, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text3(getString(rs, columnIndex));
        }
    },
    TEXT4(14, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text4(getString(rs, columnIndex));
        }
    },
    TEXT5(15, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text5(getString(rs, columnIndex));
        }
    },
    TEXT6(16, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text6(getString(rs, columnIndex));
        }
    },
    TEXT7(17, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text7(getString(rs, columnIndex));
        }
    },
    TEXT8(18, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text8(getString(rs, columnIndex));
        }
    },
    TEXT9(19, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text9(getString(rs, columnIndex));
        }
    },
    TEXT10(20, TEXT) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.text10(getString(rs, columnIndex));
        }
    },

    QTY1(21, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty1(getDouble(rs, columnIndex));
        }
    },
    QTY2(22, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty2(getDouble(rs, columnIndex));
        }
    },
    QTY3(23, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty3(getDouble(rs, columnIndex));
        }
    },
    QTY4(24, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty4(getDouble(rs, columnIndex));
        }
    },
    QTY5(25, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty5(getDouble(rs, columnIndex));
        }
    },
    QTY6(26, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty6(getDouble(rs, columnIndex));
        }
    },
    QTY7(27, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty7(getDouble(rs, columnIndex));
        }
    },
    QTY8(28, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty8(getDouble(rs, columnIndex));
        }
    },
    QTY9(29, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty9(getDouble(rs, columnIndex));
        }
    },
    QTY10(30, NUMERIC) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.qty10(getDouble(rs, columnIndex));
        }
    },

    DATE1(31, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date1(getLong(rs, columnIndex));
        }
    },
    DATE2(32, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date2(getLong(rs, columnIndex));
        }
    },
    DATE3(33, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date3(getLong(rs, columnIndex));
        }
    },
    DATE4(34, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date4(getLong(rs, columnIndex));
        }
    },
    DATE5(35, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date5(getLong(rs, columnIndex));
        }
    },
    DATE6(36, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date6(getLong(rs, columnIndex));
        }
    },
    DATE7(37, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date7(getLong(rs, columnIndex));
        }
    },
    DATE8(38, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date8(getLong(rs, columnIndex));
        }
    },
    DATE9(39, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date9(getLong(rs, columnIndex));
        }
    },
    DATE10(40, DATE) {
        @Override
        void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException {
            builder.date10(getLong(rs, columnIndex));
        }
    }
    ;

    private final int id;
    private final DDSFieldType fieldType;

    abstract void build(RecordDb.RecordDbBuilder builder, ResultSet rs, int columnIndex) throws SQLException;

    public static DDSFieldId from(int id) {
        for (DDSFieldId value : values()) {
            if (value.getId() == id) {
                return value;
            }
        }

        throw new InvalidParameterException(String.format("Invalid dds field id: %s", id));
    }
}
